/**
 * 作者：陈文斌 on 2019/1/26.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/model/main_scoped_model.dart';
import 'package:crm/model/PainPoint/pain_point_model.dart';
import 'package:crm/viewModel/PainPoint/pain_point_view_model.dart';
import 'package:crm/view/Refresh/refresh_footer_normal.dart';
import 'package:crm/component/PainPoint/pain_point_record_row_section.dart';
import 'package:crm/page/PainPoint/pain_point_record_detail_page.dart';

class PainPointRecordListSection extends StatefulWidget {
  final String customerId;

  PainPointRecordListSection({Key key, this.customerId}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _PainPointRecordListSectionState();
  }
}

class _PainPointRecordListSectionState
    extends State<PainPointRecordListSection> {
  ScrollController _listController = ScrollController();
  List<PainPointModel> _painPointRecords = List<PainPointModel>();
  bool _isPerformingRequest = false;
  int _page = 0;
  bool _isMore = true;

  _get(page) async {
    if (!_isPerformingRequest) {
      _isPerformingRequest = true;
      PainPointVM().searchPainPointRecord(
        page,
        _painPointRecords,
        customerId: widget.customerId,
        callback: (visitorRecord, bool isMore) {
          // 请求成功
          _isMore = isMore;
          _isPerformingRequest = false;
          _painPointRecords = visitorRecord;

          if (!mounted) return;
          setState(() {});
        },
        errorCallback: (errMsg) => print(errMsg),
      );
    }
  }

  @override
  void initState() {
    super.initState();
    _get(_page);
    _listController.addListener(() {
      if (_listController.position.pixels ==
          _listController.position.maxScrollExtent) _get(++_page);
    });
  }

  @override
  void dispose() {
    super.dispose();
    _listController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    MainModel mainModel = MainModel().of(context);
    return RefreshIndicator(
      displacement: 20.0,
      child: createListView(mainModel),
      onRefresh: () async {
        _page = 0;
        _get(_page);
      },
    );
  }

  Widget createListView(MainModel mainModel) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 15),
      child: ListView(
        children: <Widget>[
          PaddingSet.PaddingTop(10),
          PainPointRecordRowSection(
            key: Key('1'),
            onTap: () {
              print('痛点详情');
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => PainPointRecordDetailPage(),
                ),
              );
            },
          ),
        ],
      ),
    );
// ListView.builder(
//      controller: _listController,
//      physics: const AlwaysScrollableScrollPhysics(),
//      itemCount: _painPointRecords.length + 1,
//      itemBuilder: (context, i) {
//        if (i < _painPointRecords.length) {
//          PainPointModel _painPointRecord = _painPointRecords[i];
//          return Column(
//            children: <Widget>[
//              Padding(padding: EdgeInsets.only(top: 10)),
//              PainPointRecordRowSection(
////                visitorRecord: _visitorRecord,
////                onTap: () {
////                  Navigator.push(
////                    context,
////                    MaterialPageRoute(
////                      builder: (context) => VisitorRecordDetailPage(),
////                    ),
////                  );
////                },
//              ),
//            ],
//          );
//        } else {
//          return RefreshFooterNormal(isMore: _isMore);
//        }
//      },
//    );
  }
}
